//
//  NetVideoListViewController.m
//  VLCPlayer
//
//  Created by mac on 2019/3/31.
//  Copyright © 2019 YangYang. All rights reserved.
//

#import "NetVideoListViewController.h"
#import "NetVideoViewController.h"
#import "VieoListViewController.h"

@interface NetVideoListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation NetVideoListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
//    [self getVideoUrlModel];
//    [self modifyFile];
    
    NSArray *pathArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:@"VideoSource"];
    NSLog(@"pathArr = %@",pathArr);
    for (NSString *urlPath in pathArr) {
        NSLog(@"urlPath = %@",urlPath);
        NSString *videoName = [[urlPath stringByDeletingPathExtension] lastPathComponent];
        NSString *videoPath = urlPath;
        VideoListModel *model = [[VideoListModel alloc] init];
        model.videoName = videoName;
        model.videoPath = videoPath;
        model.downloadFolder = videoName;
        [self.dataSource addObject:model];
    }
    NSLog(@"self.dataSource = %@",self.dataSource);
}

-(void)getVideoUrlModel
{
//    NSString *videoUrlPath = [[NSBundle mainBundle] pathForResource:@"射雕英雄传_2017版.plist" ofType:@""];
    
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"知否知否应是绿肥红瘦.plist" ofType:@""];
    NSArray *sourceArr = [NSArray arrayWithContentsOfFile:filePath];
    NSMutableArray *videoArr = [NSMutableArray new];
    
    for (NSInteger i = 1; i < sourceArr.count + 1; i++) {
        NSMutableDictionary *VideoDic = [NSMutableDictionary new];
        NSString *urlString = sourceArr[i - 1];
        
        NSString *videoName = [[urlString stringByDeletingPathExtension] lastPathComponent];
        videoName = [videoName stringByReplacingOccurrencesOfString:@"ZF" withString:@"知否"];
        VideoDic[@"videoName"] = videoName;
//        VideoDic[@"videoName"] = [NSString stringWithFormat:@"知否知否应是绿肥红瘦_%.2ld",i];
        VideoDic[@"extensionName"] = [urlString pathExtension];
        VideoDic[@"VideoFullName"] = [NSString stringWithFormat:@"%@.%@",VideoDic[@"videoName"],VideoDic[@"extensionName"]];
        VideoDic[@"videoURL"] = urlString;
        VideoDic[@"writer"] = @"冯绍峰、赵丽颖、朱一龙、施诗";
        [videoArr addObject:VideoDic];
    }
    NSLog(@"videoArr = %@",videoArr);
    NSString *bj_xr = DownloadFilePath(@"知否知否应是绿肥红瘦.plist");
    NSLog(@"cqz = %@",bj_xr);
    [videoArr writeToFile:bj_xr atomically:YES];

}

-(void)modifyFile
{
    NSArray *pathArr = [[NSBundle mainBundle] pathsForResourcesOfType:@"plist" inDirectory:@"VideoSource"];
    NSLog(@"pathArr = %@",pathArr);
    for (NSString *urlPath in pathArr) {
        NSLog(@"urlPath = %@",urlPath);
        NSString *videoName = [[urlPath stringByDeletingPathExtension] lastPathComponent];
        NSString *videoPath = urlPath;
        NSArray *videoArr = [NSArray arrayWithContentsOfFile:videoPath];
        NSLog(@"videoArr = %@",videoArr);
        NSMutableArray *videoDicArr = [NSMutableArray new];
        for (NSDictionary *dic in videoArr) {
            NSMutableDictionary *tempDic = [NSMutableDictionary dictionaryWithDictionary:dic];
            tempDic[@"downloadFolder"] = videoName;
            [videoDicArr addObject:tempDic];
        }
        NSString *plistName = [NSString stringWithFormat:@"%@.plist",videoName];
        NSString *plistPath = DownloadFilePath(plistName);
        NSLog(@"plistPath = %@",plistPath);
        [videoDicArr writeToFile:plistPath atomically:YES];
    }
}

-(void)createTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) style:UITableViewStylePlain];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
//    NSDictionary *videoInfo = _dataSource[indexPath.row];
    VideoListModel *model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.videoName;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    NSDictionary *videoInfo = _dataSource[indexPath.row];
    VideoListModel *model = self.dataSource[indexPath.row];
    
    VieoListViewController *netVideoVC = [[VieoListViewController alloc] init];
    netVideoVC.videoPath = model.videoPath;
    netVideoVC.navigationItem.title = model.videoName;
    [self.navigationController pushViewController:netVideoVC animated:YES];
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSource.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(NSMutableArray *)dataSource
{
    if (!_dataSource) {
        _dataSource = [NSMutableArray new];
    }
    return _dataSource;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
